<html>
<head>
<style type="text/css">

table{	
border-collapse:collapse;
table-layout:fixed;		
}
td.info
{
padding-right:10px;
}
tr.tables
{
border:1px solid darkgrey;
background-color:black;
color:white;
}

tr.attributes
{
border:1px solid white;
background-color:#303030;
color:white;
}
tr.rowa
{
border:1px solid white;
background-color:#D0D0D0;
color:black;
}
tr.rowb
{
border:1px solid white;
background-color:white;
color:black;
}
</style>
</head>
<body>

<?php
$id=$_GET["id"]; // SANITIZE THIS

print "<div align=center><form action=search.php method=post>";
print "<input type=text maxlength=100 name=query size='150' value=\"". htmlspecialchars($raw_query)."\"></input>";
print "<input type=submit  value=Run></input></form><br>";

//SOuRCE FOR LINKS HERE
print "<div align=center><table width=800>
		<tr class=rowb>
		<td><a href=addmovie.php><b>Add Movie</b></a></td>
		<td><a href=addmovieactor.php><b>Add Movie Actor</b></a></td>
		<td><a href=addmoviedirector.php><b>Add Movie Director</b></a></td>
		<td><a href=add_actor_director.php><b>Add Actor or Director</b></a>	</td>
		</tr>
		</table></div>";

function connect_to_server(){
	$DB="CS143";
	$db_connection = mysql_connect("localhost", "cs143", "");
	if(!$db_connection) {
		$errmsg = mysql_error($db_connection);
		print "Connection failed: " .$errmsg ."<br />";
		exit(1);
	}
	mysql_select_db("$DB", $db_connection);
	return $db_connection;

}

function make_query($command, $db_connection){
		
	$resTable = mysql_query($command, $db_connection);
	if(!$resTable) {
		$errmsg = mysql_error($db_connection);

		if($command != ""){
			print "Query fetch failed: " .$errmsg ."<br />";
		}

		exit(1);
	
	}
	return $resTable;
	
}

$conn=connect_to_server();

$query = "SELECT last,first,sex,dob,dod  FROM Actor WHERE id=%d;";
if(!preg_match("/^[0-9]+$/", $id)){
		print "<h1>Invalid Id</h1>
			   <img src='data/error.png'/><br>
			   <b>The id \"$id\" is not an acceptable id.</b>
			   ";
		exit();
}
$query_filled = sprintf($query, $id);
$res=make_query($query_filled, $conn);
$row = mysql_fetch_row($res);
if($row== null){
		print "<h1>Actor Not Found</h1>
			   <img src='data/filenotfound.png'/><br>
			   <b>The Actor with the id $id does not exist! please select another actor.</b>";
	
	
		exit();
}
$data["last"] = $row[0];
$data["first"] = $row[1];
$data["name"] = $data["first"] . " " . $data["last"];
$data["sex"] = $row[2];
$data["dob"] = $row[3];
$data["dod"] = $row[4];


$query = "SELECT MovieActor.mid,MovieActor.role,Movie.title FROM MovieActor, Movie WHERE MovieActor.aid=%d AND MovieActor.mid = Movie.id;";
$query_filled = sprintf($query, $id);
$res=make_query($query_filled, $conn);



$data["movies"] = array();
$i=0;
while($row = mysql_fetch_row($res)){
	$data["movies"][$i]["id"] =  $row[0] ;
	$data["movies"][$i]["role"] = $row[1];
	$data["movies"][$i]["title"] = $row[2];
	$i++;
}


$directorphp="director.php";
$genrephp="genre.php";
$actorphp="actor.php";
$moviephp="movie.php";

if($data["sex"] == "Male"):
	$srcimg = 'data/actor-male.png';
elseif($data["sex"] == "Female"):
	$srcimg = 'data/actor-female.png';
else:
	$srcimg = 'data/question.png';
endif;

print "<br>";
print "
<table>

<tr><h3>$data[name]</h3></tr>

<tr><td><img src=\"$srcimg\"/></td><td>
<table>

<tr class=rowa>
<td class = info>Sex</td>
<td>$data[sex]</td>
</tr>

<tr class=rowb>
<td class = info>Date of Birth</td>
<td>$data[dob]</td>
</tr>

<tr class=rowa>
<td class = info>Date of Death</td>
<td>$data[dod]</td>
</tr>

</table>
</td></tr>
</table>
";

print "<h3>Movies</h3><table>";
for($i=0; $i < sizeof($data["movies"]); $i++){
	
	if($i%2==0)
		$type="rowa";
	else
		$type="rowb";
		
	print "<tr class=$type><td class = info>
			<a href=$moviephp?id=".$data[movies][$i][id].">".$data[movies][$i][title]."</a>
			</td><td class = info>".$data[movies][$i][role]."</td></tr>";	
}
print "<tr><td></td><td><a href=addmovieactor.php?aid=".$id.">Add Roles</a></td></tr>";
print "</table>";
mysql_close($conn);
print "</div>";

?>

<br>
</body>
</html>
